import 'package:dynamic_stock/const/const.dart';
import 'package:dynamic_stock/model/company_user_model.dart';
import 'package:dynamic_stock/model/login_model.dart';
import 'package:dynamic_stock/network/network.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class CompanyUserPage extends StatefulWidget {
  
  final String stewardComId;

  const CompanyUserPage({Key key, @required this.stewardComId}) : super(key: key);

  @override
  _CompanyUserPageState createState() => _CompanyUserPageState();
}

class _CompanyUserPageState extends State<CompanyUserPage> {
  
  RefreshController _refreshController;
  List<CompanyUserModel> list = [];
  int pageIndex = 1;
  int pageSize = 10;

  @override
  initState(){
    super.initState();
    _refreshController = RefreshController(initialRefresh: true);
  }

  @override
  dispose(){
    _refreshController.dispose();
    super.dispose();
  }

  _loadData() async {
    pageIndex = 1;
    UserData userData = await UserData.instance();
    NetworkTool().request_company_user(
      {"platComId":userData.platComId,"stewardComId":widget.stewardComId,"pageIndex":pageIndex, "pageSize":pageSize}
    ).then((res){
      if(res.errcode == 0 && res.data.list != null) {
        setState(() {
          list = res.data.list;
        });
      } else {
        print(res.message);
      }
    }).catchError((e){

    }).whenComplete((){
      if(_refreshController.isLoading) _refreshController.loadComplete();
      if(_refreshController.isRefresh) _refreshController.refreshCompleted(resetFooterState: true);
    });
  }

  _onLoadindMore() async{
    UserData user = await UserData.instance();
    pageIndex += 1;
    NetworkTool().request_company_user(
      {"platComId":user.platComId,"stewardComId":widget.stewardComId,"pageIndex":pageIndex, "pageSize":pageSize}
    ).then((res){
      if(res.errcode == 0) {
        setState(() {
          list.addAll(res.data.list);
        });
        if(_refreshController.isLoading) _refreshController.loadComplete();
        if(res.data.list.length < pageSize) _refreshController.loadNoData();
      } else {
        print(res.message);
      }
    }).catchError((e){
      if(_refreshController.isLoading) _refreshController.loadComplete();
    }).whenComplete((){
      if(_refreshController.isRefresh) _refreshController.refreshCompleted();
    });
  }


  Widget _createItem(CompanyUserModel model) {
    return Container(
        color: Colors.white,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.only(top: 28, left: 25),
              child: Row(
                children: <Widget>[
                  Image.asset("assets/user_icon.png"),
                  SizedBox(width: 11.5,),
                  Text(model.stewardUserName, style: TextStyle(color: Color(0xff35343D), fontSize: 17, fontWeight: FontWeight.bold),)
                ],
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(left: 55, top: 5),
              child: Text(model.stewardComName,style: TextStyle(color: Color(0xff5F5D70), fontSize: 13),),
            ),
            Padding(
              padding: const EdgeInsets.fromLTRB(55, 14.5, 19.5, 25),
              child: Column(
                children: <Widget>[
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Text("登录账号",style: TextStyle(color: Color(0xffB2B0C0), fontSize: 14),),
                      Text(model.stewardUserAccount,style: TextStyle(color: Color(0xff5F5D70), fontSize: 14),),
                    ],
                  ),
                  SizedBox(height: 9,),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Text("创建时间",style: TextStyle(color: Color(0xffB2B0C0), fontSize: 14),),
                      Text(model.stewardRegisterTime,style: TextStyle(color: Color(0xff5F5D70), fontSize: 14),),
                    ],
                  ),
                ],
              )
            ),
            Container(height: 10,color: AppMainColor.normalBackColor,)
          ],
        ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        leading: FlatButton(
          child: Image.asset("assets/system-backnew.png"),
          onPressed: (){
            Navigator.of(context).pop();
          },
        ),
        title: Text("车管家用户", style: AppTextStyle.tabbarTitleStyle),
        elevation: 0.5,
      ),
      body: Container(
        color: AppMainColor.normalBackColor,
        child: SmartRefresher(
          enablePullDown: true,
          enablePullUp: true,
          header: WaterDropHeader(),
          footer: CustomFooter(
          builder: (BuildContext context,LoadStatus mode){
              Widget body ;
              if(mode==LoadStatus.idle){
                body =  Text("上拉加载更多");
              }
              else if(mode==LoadStatus.loading){
                body =  CupertinoActivityIndicator();
              }
              else if(mode == LoadStatus.failed){
                body = Text("加载失败请重试!");
              }
              else if(mode == LoadStatus.canLoading){
                  body = Text("松开加载更多");
              }
              else{
                body = Text("已经到底了");
              }
              return Container(
                height: 55.0,
                child: Center(child:body),
              );
            },
          ),
          controller: _refreshController,
          onRefresh: _loadData,
          onLoading: _onLoadindMore,
          child: ListView.builder(
            itemCount: list.length,
            itemBuilder: (context, index){
              return _createItem(list[index]);
            },
          ),
        ),
      ),
    );
  }
}